package com.cwh.taskcenter.handler;

import com.alibaba.cola.dto.Response;
import com.alibaba.cola.dto.SingleResponse;
import com.alibaba.cola.exception.BizException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @author cwh
 */
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {

    /**
     * 处理业务异常
     */
    @ExceptionHandler(BizException.class)
    public SingleResponse<?> handleBizException(BizException e) {
        log.warn("业务异常: errCode={}, errMessage={}", e.getErrCode(), e.getMessage(), e);
        return SingleResponse.buildFailure(e.getErrCode(), e.getMessage());
    }

    /**
     * 处理其他未捕获异常
     */
    @ExceptionHandler(Exception.class)
    public Response handleException(Exception e) {
        log.error("系统异常: ", e);
        return SingleResponse.buildFailure("SYS_ERROR", "系统繁忙，请稍后再试");
    }
}
